package com.bbm.communitysystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.UserAddr;
import com.bbm.communitysystem.domain.UserInfo;
import com.bbm.communitysystem.domain.dto.UserAddrDto;
import com.bbm.communitysystem.domain.vo.UserAddrVo;
import com.bbm.communitysystem.service.IUserAddrService;
import com.bbm.communitysystem.service.IUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-28
 */
@Tag(name = "用户地址管理")
@RestController
@RequestMapping("/user-addr")
@RequiredArgsConstructor
public class UserAddrController {

    private final IUserAddrService userAddrService;

    private final IUserInfoService userInfoService;

    @Operation(summary = "新增用户地址")
    @PostMapping("/")
    public R<String> addUserAddr(@RequestBody UserAddrDto userAddrDto) {

        UserAddr r = BeanUtil.copyProperties(userAddrDto, UserAddr.class);

        r.setCreateBy(userAddrDto.getUpdateBy());
        r.setCreateTime(System.currentTimeMillis() + "");
        r.setUpdateTime(r.getCreateTime());

        boolean save = userAddrService.save(r);

        return save ? R.ok("新增成功") : R.error("新增失败");
    }

    @Operation(summary = "查询所有地址")
    @GetMapping("/")
    public R<List<UserAddr>> getAllAddrs() {
        return R.ok(userAddrService.list());
    }

    @Operation(summary = "查询单个地址")
    @GetMapping("/{id}")
    @Parameter(name = "id", description = "地址id")
    public R<UserAddrVo> getAddrById(@PathVariable Long id) {
        return R.ok(getUserAddrVo(userAddrService.getById(id)));
    }

    @Operation(summary = "查询用户地址")
    @GetMapping("/user/{userId}")
    public R<List<UserAddrVo>> getAddrByUserId(@PathVariable Long userId) {
        List<UserAddrVo> list = userAddrService.lambdaQuery()
                .eq(UserAddr::getUserId, userId)
                .list()
                .stream()
                .map(this::getUserAddrVo).toList();

        return R.ok("获取成功", list);
    }

    @Operation(summary = "修改地址信息")
    @PutMapping("/")
    public R<String> updateAddr(@RequestBody UserAddrDto dto) {

        boolean update = userAddrService.lambdaUpdate()
                .set(dto.getAddr() != null && !dto.getAddr().isEmpty(), UserAddr::getAddr, dto.getAddr())
                .set(dto.getContact() != null && !dto.getContact().isEmpty(), UserAddr::getContact, dto.getContact())
                .set(dto.getRemark() != null && !dto.getRemark().isEmpty(), UserAddr::getRemark, dto.getRemark())

                .set(UserAddr::getUpdateBy, dto.getUpdateBy())
                .set(UserAddr::getUpdateTime, System.currentTimeMillis() + "")
                .eq(UserAddr::getUserId, dto.getUserId())
                .eq(UserAddr::getId, dto.getId())
                .update();

        return update ?
                R.ok("更新成功") : R.error("更新失败");
    }

    @Operation(summary = "删除地址")
    @DeleteMapping("/{id}")
    @Parameter(name = "id", description = "地址id")
    public R<String> deleteAddr(@PathVariable Long id) {
        return userAddrService.removeById(id) ?
                R.ok("删除成功") : R.error("删除失败");
    }

    private UserAddrVo getUserAddrVo(UserAddr userAddr) {
        UserAddrVo userAddrVo = BeanUtil.copyProperties(userAddr, UserAddrVo.class);
        UserInfo byId = userInfoService.getById(userAddr.getUserId());
        userAddrVo.setUserNick(byId.getUserNick());
        userAddrVo.setUserId(byId.getId());
        userAddrVo.setRemark(userAddrVo.getRemark());
        userAddrVo.setContact(userAddrVo.getContact());

        return userAddrVo;
    }

}
